#ifndef NET_SOCKADDR_H
#define NET_SOCKADDR_H

#include "utility/Copyable.h"

#include <netinet/in.h>
#include <string>

/// @code
/// sockaddr_in @c _addr
/// |- sa_family_t @c sin_family
/// |- in_port_t @c sin_port
/// |- struct in_addr @c sin_addr
///    |- in_addr_t @c s_addr
/// | - unsigned char @c sin_zero[]
/// can convert to
/// sockaddr
/// | - sa_family_t @c sa_family
/// | - char @c sa_data[14]


class Sockaddr final {
public:
    explicit Sockaddr(uint16_t port = 0, bool loopbackOnly = false);
    Sockaddr(const char *ip, uint16_t port);
    explicit Sockaddr(const struct sockaddr_in addr): _addr{addr} {}
    ~Sockaddr() = default;

    COPYABLE(Sockaddr);

    sa_family_t family() const { return _addr.sin_family; }
    std::string IP() const;
    std::string IP_port() const;
    const struct sockaddr *sock_addr() const;

    void set_sock_addr(const struct sockaddr_in &addr) { _addr = addr; }

private:
    struct sockaddr_in _addr;
};

#endif